** This file replicates Figures 3.3, 3.8, and 3.10

* Open dataset on contributions to candidate from various sources provided by 
* NIMSP
use newnimsp, clear

* Keep only years between 1995 and 2011, where coverage of ideological data is best
keep if year>1995 & year<2011

* Rename incumbency variable and keep only incumbents
ren incumbency_statusid ico
keep if ico=="I"

* Rename district variable and drop candidates who are missing on chamber or dist
* because those are candidates for non-legislative offices
ren district dist 
drop if chamber==.
drop if dist==.

* Merge in data from Shor/McCarty ideal point scores for state legislators
sort statabbr dist year chamber
merge statabbr dist year chamber using npat1996_2010

* Drop cases that did not merge in 
drop if _m==2

* Drop NE because of multi-member districts. Drop WA because of multi-member districts.
* Drop NH because districts are not coded in comparable ways in each dataset
drop if statabbr=="NE" | statabbr=="WA" | statabbr=="NH"
drop _m

* Generate variable indicating which states have robust public funding and drop those states
gen pubfundstate=1 if statabbr=="AZ" & year>1998
replace pubfundstate=1 if statabbr=="CT" & year>2006
replace pubfundstate=1 if statabbr=="ME" & year>1998
replace pubfundstate=1 if statabbr=="MN"
recode pubfundstate .=0
drop if pubfundstate==1

* Merge in dataset on campaign finance laws
sort statabbr year
merge statabbr year using masterdataset

* Calculate total amounts given by each sector in each state-election cycle
bysort statabbr year: egen partytotal=sum(parties)
bysort statabbr year: egen indtotal=sum(individuals+law_lob)
bysort statabbr year: egen issuetotal=sum(issuegroups)
bysort statabbr year: egen unionstotal=sum(unions)
bysort statabbr year: egen businesstotal=sum(business)

* Generate count of how many legislatures there are in each state-election cycle
gen number=1 
bysort statabbr year: egen totallegs=sum(number)

* Create weight variable so that analyses will be weighted according to how many 
* incumbents are running in that legislature in that particular year  
gen weight=100/number

* Generate an indicator of which legislatures are in the Top 20 most professional
gen professional2=1 if statabbr=="CA" | statabbr=="NY" | statabbr=="PA" | statabbr=="MI" | statabbr=="IL" | statabbr=="MA" | statabbr=="WI" | statabbr=="OH" | statabbr=="FL" | statabbr=="NJ" | statabbr=="AK" | statabbr=="DE" | statabbr=="MN" | statabbr=="TX" | statabbr=="WA" | statabbr=="CT" | statabbr=="HI" | statabbr=="NC" | statabbr=="SC" | statabbr=="AZ" 

* Trim low values so that any negative values are re-coded to 0
replace prop_from_party=0 if prop_from_party<0
replace prop_from_ind=0 if prop_from_ind<0
replace prop_from_business=0 if prop_from_business<0
replace prop_from_issue=0 if prop_from_issue<0
replace prop_from_unions=0 if prop_from_unions<0

* Create variables that indicate the proportion of the total amount given by parties
* that when to a particular candidate
gen prop_of_party=parties/partytotal
gen prop_of_ind=individuals/indtotal
gen prop_of_issue=issuegroups/issuetotal
gen prop_of_business=business/businesstotal
gen prop_of_union=unions/unionstotal

* Trim values so that negative values are re-coded to 0
replace prop_of_party=0 if prop_of_party<0
replace prop_of_ind=0 if prop_of_ind<0
replace prop_of_business=0 if prop_of_business<0
replace prop_of_issue=0 if prop_of_issue<0
replace prop_of_union=0 if prop_of_union<0
replace prop_of_dem=0 if prop_of_dem<0
replace prop_of_rep=0 if prop_of_rep<0

* Create Figure 3.3
twoway lpolyci prop_of_party np_score if l_party_cand==0 & np_score>-2 & np_score<2 , bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off)  || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("By Parties") saving(byparty.gph, replace)
twoway lpolyci prop_of_ind np_score if indlims==0 & np_score>-2 & np_score<2 , bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off) || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("By Individuals") saving(byind.gph, replace)
twoway lpolyci prop_of_issue np_score if orglims==0 & np_score>-2 & np_score<2 , bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off) || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("By Issue Groups") saving(byissue.gph, replace)
twoway lpolyci prop_of_business np_score if orglims==0 & np_score>-2 & np_score<2 , bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off)  || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("By Business Groups")  saving(bybusiness.gph, replace)
twoway lpolyci prop_of_union np_score if orglims==0 & np_score>-2 & np_score<2 , bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off)  || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("By Unions")  saving(byunions.gph, replace)
graph combine byparty.gph byind.gph byissue.gph bybusiness.gph byunions.gph, graphr(c(white)) ycom 
graph export fig_3_3.eps, replace

* Create Figure 3.8
twoway lpolyci prop_from_party np_score if l_party_cand==0 & orglims==0 & indlims==0 & np_score>-2 & np_score<2 [aw=weight], bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off) || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("From Parties") saving(fromparty.gph, replace)
twoway lpolyci prop_from_ind np_score if l_party_cand==0 & orglims==0 & indlims==0 & np_score>-2 & np_score<2 [aw=weight], bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off)  || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("From Individuals") saving(fromind.gph, replace)
twoway lpolyci prop_from_issue np_score if l_party_cand==0 & orglims==0 & indlims==0 & np_score>-2 & np_score<2 [aw=weight], bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off)  || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("From Issue Groups") saving(fromissue.gph, replace)
twoway lpolyci prop_from_business np_score if l_party_cand==0 & orglims==0 & indlims==0 & np_score>-2 & np_score<2 [aw=weight], bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off)  || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("From Business Groups")  saving(frombusiness.gph, replace)
twoway lpolyci prop_from_unions np_score if l_party_cand==0 & orglims==0 & indlims==0 & np_score>-2 & np_score<2 [aw=weight], bw(.4) scheme(s1mono) level(90) aspect(1)  xtitle("Ideology", size(large)) legend(off)  || histogram np_score  if l_party_cand==0  & orglims==0 & indlims==0 & np_score>-2 & np_score<2, frac fc(none) discrete title("From Unions")  saving(fromunions.gph, replace)
graph combine fromparty.gph fromind.gph fromissue.gph frombusiness.gph fromunions.gph, graphr(c(white)) 
graph export fig_3_8.eps, replace

* Create Figure 3.10
twoway lpolyci prop_from_party np_score if l_party_cand==0 & np_score>-2 & np_score<2 , bw(.4) scheme(s1mono) aspect(1) ytitle("Proportion of Money from Party", size(large)) xtitle("Ideology", size(large)) legend(off) || histogram np_score  if l_party_cand==0 & np_score>-2 & np_score<2 , frac fc(none) discrete saving(nopartylimits.gph, replace) title("States without Party Limits", size(vlarge))
twoway lpolyci prop_from_party np_score if l_party_cand==1 & np_score>-2 & np_score<2 , bw(.4) scheme(s1mono) aspect(1) ytitle("Proportion of Money from Party", size(large)) xtitle("Ideology", size(large)) legend(off) || histogram np_score  if l_party_cand==1 & np_score>-2 & np_score<2 , frac fc(none) discrete saving(partylimits.gph, replace)  title("States with Party Limits", size(vlarge))
graph combine nopartylimits.gph partylimits.gph, ycom graphr(c(white))
graph export fig_3_10.eps, replace



